﻿using Studio77.FruitLinkLink2.Core;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Media;

namespace Studio77.FruitLinkLink2.Util
{
    public class AudioPlayHelper
    {
        static Popup popUp;
        private static MediaElement backgroundPlayer;

        static Popup popUp2;
        private static MediaElement effectPlayer;

        public static void PlayEffectSound(string fileName)
        {
            if (!StageManager.Instance.isPlaySound)
            {
                return;
            }
            popUp2 = new Popup();
            effectPlayer = new MediaElement();
            effectPlayer.Source = null;
            popUp2.Child = effectPlayer;
            popUp2.IsOpen = true;
            try
            {
                effectPlayer.Source = new Uri(@" ms-appx:///Audio/" + fileName, UriKind.Absolute);
                effectPlayer.Volume = 1.0;
                effectPlayer.Play();
            }
            catch (Exception)
            {
                effectPlayer.Source = null;
                effectPlayer = null;
            }
        }

        public static void PlayBackground(string fileName)
        {
            if (!StageManager.Instance.isPlaySound)
            {
                return;
            }
            popUp = new Popup();
            backgroundPlayer = new MediaElement();
            backgroundPlayer.Source = null;
            popUp.Child = backgroundPlayer;
            popUp.IsOpen = true;
            try
            {
                backgroundPlayer.Source = new Uri(@" ms-appx:///Audio/" + fileName, UriKind.Absolute);
                backgroundPlayer.Volume = 1.0;
                backgroundPlayer.Play();
                backgroundPlayer.IsLooping = true;
            }
            catch (Exception)
            {
                backgroundPlayer.Source = null;
                backgroundPlayer = null;

            }
        }

        public static void StopBackground()
        {
            if (backgroundPlayer != null && backgroundPlayer.CurrentState == MediaElementState.Playing)
            {
                try
                {
                    backgroundPlayer.Source = null;
                    backgroundPlayer.Stop();
                }
                catch (Exception)
                {
                    //throw;
                }
            }
        }
    }
}
